<?php

/**
* ShowCodeTest.php
*
* Show slide test
*
* @author     Blazej Zieba <b@zieba.info>
* @copyright  2007 konekto.com
* @version    CVS: $Id:$
*/
require_once '../framework/config.php';
require_once 'simpletest/autorun.php';
require_once 'simpletest/web_tester.php';
require_once '../ShowCode.class.php';
require_once '../framework/Yt.class.php';

class  ShowCodeTest extends WebTestCase
{
    private $testName;
    private $id;
    private $testDesc,$testGeoCode;

    public function setUp() 
    {

        $global = &PEAR::getStaticProperty('CONFIG_INI','GLOBAL');
        $upload = &PEAR::getStaticProperty('CONFIG_INI','UPLOAD');
        $this->url = $global['service_url'];

        $this->testName = md5(uniqid(rand(), true));
        $this->testDesc = md5(uniqid(rand(), true));
        $this->testGeoCode = "Mirsk,PL";        
        $this->testName = md5(uniqid(rand(), true));
        
        $locale =  &PEAR::getStaticProperty('CONFIG_INI','LOCALE');
        $this->locale = $locale['default_locale']; 


        $options = &PEAR::getStaticProperty('CONFIG_INI','DSN');  

  
        $dbh = new PDO($options['dsn_string']);        
        $dbh->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );

        $dbh->exec("INSERT INTO guide (geocode,publish) values ('".$this->testGeoCode."','1')");
        $this->id = $dbh->lastInsertId();

        $sth = $dbh->prepare('INSERT INTO guide_name (guide_id,name,locale) VALUES (:guide_id,:name,:locale) ');
        $sth -> bindValue(':guide_id', $this->id ,PDO::PARAM_INT);
        $sth -> bindValue(':locale',  $this->locale ,PDO::PARAM_STR);
        $sth -> bindValue(':name',  $this->testName ,PDO::PARAM_STR);
        $sth->execute();

        $sth = $dbh->prepare('INSERT INTO guide_desc (guide_id,desc,locale) VALUES (:guide_id,:desc,:locale) ');
        $sth -> bindValue(':guide_id', $this->id ,PDO::PARAM_INT);
        $sth -> bindValue(':locale',  $this->locale ,PDO::PARAM_STR);
        $sth -> bindValue(':desc',  $this->testDesc ,PDO::PARAM_STR);
        $sth->execute();
        
        $dbh->exec("UPDATE  guide SET  video_file = 'jNQXAC9IVRw' WHERE rowid=".$this->id);

        $dbh->exec("UPDATE guide SET  pic_file = '".$this->id."image.jpg' WHERE rowid=".$this->id);
        copy( dirname(__FILE__).'/image.jpg', $upload['upload_dir'].$this->id.'image.jpg');

        $dbh->exec("INSERT INTO guide (geocode,publish) values ( '".$this->testGeoCode."','0')");        
        $this->notPublishedId = $dbh->lastInsertId();

        $sth = $dbh->prepare('INSERT INTO guide_name (guide_id,name,locale) VALUES (:guide_id,:name,:locale) ');
        $sth -> bindValue(':guide_id', $this->notPublishedId ,PDO::PARAM_INT);
        $sth -> bindValue(':locale',  $this->locale ,PDO::PARAM_STR);
        $sth -> bindValue(':name',  'notpublish'.$this->testName ,PDO::PARAM_STR);
        $sth->execute();

        $sth = $dbh->prepare('INSERT INTO guide_desc (guide_id,desc,locale) VALUES (:guide_id,:desc,:locale) ');
        $sth -> bindValue(':guide_id', $this->notPublishedId ,PDO::PARAM_INT);
        $sth -> bindValue(':locale',  $this->locale ,PDO::PARAM_STR);
        $sth -> bindValue(':desc',  $this->testDesc ,PDO::PARAM_STR);
        $sth->execute();
        

        $this->addHeader("User-Agent: Mozilla/5.0(SymbianOS/9.2; U; Series60/3.1 NokiaE51/1.00.000; Profile/MIDP-2.0 Configuration/CLDC-1.1;)AppleWebKit/413(KHTML,like Gecko)Safari/413");
        $this->get($this->url.'?cmd=ShowCode&id='.$this->id); 
        
    }

    public function testShowCode()
    {
        $upload = &PEAR::getStaticProperty('CONFIG_INI','UPLOAD');
        $global = &PEAR::getStaticProperty("CONFIG_INI","GLOBAL");        
        $this->assertTitle('Polowanie na kody');
        $this->assertText($this->testName);
        $this->assertPattern('/send_image.php\?id='.$this->id.'image.jpg'.'/');
        $this->assertText($this->testDesc);        
        // create a new VideoEntry object

        $yt = Yt::getClient();
        $videoEntry = new Zend_Gdata_YouTube_VideoEntry();
        $videoEntry = $yt->getVideoEntry('jNQXAC9IVRw');
        foreach ($videoEntry->mediaGroup->content as $content) {
            if ($content->type === "video/3gpp" && $content->format === '6') {
                $this->assertPattern('/rtsp.*video.3gp.*>Pobierz video<\/a>/');
            }
        }

        $this->assertText($this->testGeoCode);
        $this->assertText("Map of ".$this->testGeoCode);
    }

    public function testMapeSize()
    {

        $this->assertTitle('Polowanie na kody');
        $this->assertPattern('/.*&size=229x320.*/');
        $this->assertText("Map of ".$this->testGeoCode);
        
    }    

    public function testDoNotShowUnpublishCode()
    {
        $this->get($this->url.'?cmd=ShowCode&id='.$this->notPublishedId); 
        $upload = &PEAR::getStaticProperty('CONFIG_INI','UPLOAD');
        $global = &PEAR::getStaticProperty("CONFIG_INI","GLOBAL");        
        $this->assertTitle('Polowanie na kody');
        $this->assertNoText('notpublish'.$this->testName);
        $this->assertNoPattern('/send_image.php\?id='.$this->id.'image.jpg'.'/');
        $this->assertNoText($this->testDesc);        
        $this->assertText('Kod nie jest poprawny');        
    }

    public function tearDown() 
    {
        $options = &PEAR::getStaticProperty('CONFIG_INI','DSN');  
        $upload = &PEAR::getStaticProperty('CONFIG_INI','UPLOAD');
  
        $dbh = new PDO($options['dsn_string']);        
        $dbh->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );

        $sth = $dbh->query("SELECT rowid,pic_file FROM guide WHERE  rowid='".$this->id."'");
        for($i=0; $result = $sth->fetch(); $i++){
            $dbh->exec("DELETE FROM guide_name WHERE guide_id='".$result['rowid']."'");
            if ( is_file($upload['upload_dir'].$result['pic_file']) )
            {
                unlink($upload['upload_dir'].$result['pic_file']);
            }
        }

        $sth = $dbh->query("SELECT rowid,pic_file FROM guide WHERE  rowid='".$this->notPublishedId."'");
        for($i=0; $result = $sth->fetch(); $i++){
            $dbh->exec("DELETE FROM guide_name WHERE guide_id='".$result['rowid']."'");
            if ( is_file($upload['upload_dir'].$result['pic_file']) )
            {
                unlink($upload['upload_dir'].$result['pic_file']);
            }
        }

        $sth = $dbh->query("SELECT rowid,pic_file FROM guide WHERE  rowid='".$this->id."'");
        for($i=0; $result = $sth->fetch(); $i++){
            $dbh->exec("DELETE FROM guide_desc WHERE guide_id='".$result['rowid']."'");
            if ( is_file($upload['upload_dir'].$result['pic_file']) )
            {
                unlink($upload['upload_dir'].$result['pic_file']);
            }
        }

        $sth = $dbh->query("SELECT rowid FROM guide WHERE  rowid='".$this->notPublishedId."'");
        for($i=0; $result = $sth->fetch(); $i++){
            $dbh->exec("DELETE FROM guide_desc WHERE guide_id='".$result['rowid']."'");
        }
        
        $dbh->exec("DELETE FROM guide WHERE rowid = ".$this->id);
        $dbh->exec("DELETE FROM guide WHERE rowid = ".$this->notPublishedId);
        $dbh->exec("DELETE FROM guide_name WHERE guide_id = ".$this->id);

        $dbh = null;
    }
    
}
?>
